/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaProducto.java
 *
 * Created on 06/04/2011, 02:12:56 AM
 */

package vistas;

import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author usuario
 */
public class VistaProducto extends javax.swing.JFrame {

    private controladores.ControladorProducto control;
    private DefaultTableModel modeloTabla;
    private Vector<String> fila;

    /** Creates new form VistaProducto */
    public VistaProducto() {
        initComponents();
        control= new controladores.ControladorProducto(this);
        addAction();
        modeloTabla= new DefaultTableModel();
        fila= new Vector<String>();
        tblIngrediente.setModel(modeloTabla);
        fila.add("Ingrediente");
        fila.add("Cantidad");
        modeloTabla.setColumnIdentifiers(fila);
        addAction();
    }

     public boolean actualizarCabtidad(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()))){
               modeloTabla.setValueAt(spnCantidad.getValue(), i, 1);
               return true;
            }
        return false;
    }


    public void agregarFila(String [] v){
        if(!actualizarCabtidad()){
                fila= new Vector<String>();
                fila.add(v[0]);
                fila.add(v[1]);
                modeloTabla.addRow(fila);
            }
    }

     public void agregarFila1(String [] v){
        fila= new Vector<String>();
        fila.add(v[0]);
        fila.add(v[1]);
        modeloTabla.addRow(fila);
    }

    public void menos(){
        int i = tblIngrediente.getSelectedRow();
        if(i != -1)
            modeloTabla.removeRow(i);
        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el regitstro que desea quitar","Calle Hambre Arena Plaza",JOptionPane.ERROR_MESSAGE);
    }

    

    public void addListener(ActionListener escuchador){
        btnSalir.addActionListener(escuchador);
        btnRegistrar.addActionListener(escuchador);
        btnCancelar.addActionListener(escuchador);
        btnMas.addActionListener(escuchador);
        btnMenos.addActionListener(escuchador);
    }
    
    
    public void addAction(){
      btnSalir.setActionCommand("Salir");
        btnMas.setActionCommand("Mas");
        btnCancelar.setActionCommand("Cancelar");
        btnMenos.setActionCommand("Menos");
        btnRegistrar.setActionCommand("Registrar");

}

    public void cargarComboIngrediente(LinkedList<String> descripciones){
        cmbIngredientes.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbIngredientes.addItem(descripciones.get(i));

        }
    }

    public void cargarComboCategoria(LinkedList<String> descripciones){
        cmbCategoria.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbCategoria.addItem(descripciones.get(i));

        }
    }



    

    public JTable getTblIngrediente() {
        return tblIngrediente;
    }

     public void limpiar(){
        txtCodigo.setText("");
        txtDescripcion.setText("");
        txtPrecio.setText("");
        cmbCategoria.setSelectedItem(0);
        cmbIngredientes.setSelectedItem(0);
        spnCantidad.setValue(0);
    }
    


     public LinkedList<JTextField> getTextos() {
        LinkedList<JTextField> v = new LinkedList<JTextField>();
        v.add(txtCodigo);
        v.add(txtDescripcion);
        v.add(txtPrecio);
        return v;
    }

    public String [] getRegistro(){
        String [] s = new String [] {""+cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()),""+getCantidadIngrediente()};

         return s;
    }

    public Integer getCantidadIngrediente() {
        return (Integer) spnCantidad.getValue();
    }

    public boolean hayVacios(){
        for (int i = 0; i < getTextos().size(); i++) {
            JTextField texto = getTextos().get(i);
            if(texto.getText().equals("")){
                JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }
        }
        return true;
    }

   public boolean validarCantidad(){
        if(getCantidadIngrediente()<=0){
                JOptionPane.showMessageDialog(new JFrame(),"Cantidad no permitida","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }

        return true;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtDescripcion() {
        return txtDescripcion;
    }

    public JTextField getTxtPrecio() {
        return txtPrecio;
    }
   


    public boolean validoNumeros(){
    try{
           if(Double.parseDouble(getTxtPrecio().getText())<=0){
                getTxtPrecio().setText("");
                JOptionPane.showMessageDialog(new JFrame(),"El precio no es valido","Cari Burguer",JOptionPane.WARNING_MESSAGE);
                return false;
           }
       }catch(NumberFormatException ex){
           JOptionPane.showMessageDialog(new JFrame(),"Debe ingresar valores numericos","Cari Burguer",JOptionPane.WARNING_MESSAGE);
           return false;
       }
    return true;
    }

    public String getTextoCmbCategoria() {
        return (String )cmbCategoria.getItemAt(cmbCategoria.getSelectedIndex());
    }

    public String getTextoCmbIngredientes() {
        return (String )cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex());
    }

    public LinkedList<String []> getInfoTabla(){
        LinkedList<String []> v = new LinkedList<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 1)};
            v.add(s);
         }
         return v;
    }



     

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblIngrediente = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblIngrediente1 = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cmbCategoria = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPrecio = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        txtCodigo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        cmbIngredientes = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        spnCantidad = new javax.swing.JSpinner();
        btnMas = new javax.swing.JButton();
        btnMenos = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registro de Productos");
        getContentPane().setLayout(null);

        tblIngrediente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblIngrediente);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(460, 270, 410, 160);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(null);

        tblIngrediente1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblIngrediente1);

        jPanel1.add(jScrollPane2);
        jScrollPane2.setBounds(20, 470, 650, 160);

        jLabel11.setFont(new java.awt.Font("Ubuntu", 1, 15));
        jLabel11.setText("Ingredientes");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(420, 270, 110, 30);

        jLabel6.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel6.setText("Codigo:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(70, 50, 130, 30);

        jLabel1.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel1.setText("Categoria:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(70, 10, 140, 32);

        jPanel1.add(cmbCategoria);
        cmbCategoria.setBounds(190, 10, 190, 30);

        jLabel2.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel2.setText("Descripcion:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(70, 90, 140, 32);

        jLabel3.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel3.setText("Precio:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(70, 130, 224, 32);

        txtPrecio.setFont(new java.awt.Font("Arial", 0, 13));
        jPanel1.add(txtPrecio);
        txtPrecio.setBounds(190, 130, 190, 30);

        txtDescripcion.setFont(new java.awt.Font("Arial", 0, 13));
        jPanel1.add(txtDescripcion);
        txtDescripcion.setBounds(190, 90, 190, 32);
        jPanel1.add(txtCodigo);
        txtCodigo.setBounds(190, 50, 190, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(30, 70, 440, 180);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setLayout(null);

        jPanel2.add(cmbIngredientes);
        cmbIngredientes.setBounds(130, 20, 170, 30);

        jLabel5.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel5.setText("Seleione");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(20, 20, 240, 30);

        jLabel7.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel7.setText("Porciones");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(20, 70, 110, 30);
        jPanel2.add(spnCantidad);
        spnCantidad.setBounds(130, 68, 36, 30);

        btnMas.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnMas.setText("+");
        jPanel2.add(btnMas);
        btnMas.setBounds(310, 20, 60, 30);

        btnMenos.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnMenos.setText("-");
        jPanel2.add(btnMenos);
        btnMenos.setBounds(310, 60, 60, 30);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(490, 140, 380, 110);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(null);

        btnRegistrar.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnRegistrar);
        btnRegistrar.setBounds(20, 30, 120, 30);

        btnCancelar.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnCancelar);
        btnCancelar.setBounds(150, 30, 120, 30);

        btnSalir.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnSalir.setText("Salir");
        jPanel3.add(btnSalir);
        btnSalir.setBounds(280, 30, 120, 30);

        getContentPane().add(jPanel3);
        jPanel3.setBounds(30, 340, 420, 80);

        jLabel4.setFont(new java.awt.Font("Monospaced", 1, 15)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(250, 243, 243));
        jLabel4.setText("Registro de Productos");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(140, 30, 230, 22);

        jLabel8.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel8.setForeground(new java.awt.Color(250, 243, 243));
        jLabel8.setText("Ingredientes");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(610, 110, 180, 22);

        jLabel9.setBackground(new java.awt.Color(25, 64, 224));
        jLabel9.setOpaque(true);
        getContentPane().add(jLabel9);
        jLabel9.setBounds(0, 0, 910, 480);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-912)/2, (screenSize.height-503)/2, 912, 503);
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaProducto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnMas;
    private javax.swing.JButton btnMenos;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategoria;
    private javax.swing.JComboBox cmbIngredientes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner spnCantidad;
    private javax.swing.JTable tblIngrediente;
    private javax.swing.JTable tblIngrediente1;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables



}
